package jia.game;

import org.apache.log4j.Logger;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import jia.core.state.BasicUiManager;
import jia.core.GameStateManager;

/**
 * Class that creates the button row that commonly appears at the bottom of
 * the main ingame UI panel.
 *
 * @author Erik Stell
 * @version 1.0
 * @see JPanel
 * @see Skinnable
 * @since 01/13/2006
 */
public class MainButtonPanel extends JPanel{
    protected Insets borderInsets = new Insets(0, 0, 0, 0);
    protected BufferedImage skinImage;
    private GameStateManager mgr;
    boolean button1Clicked = false;
    private static final Logger log = Logger.getLogger(MainButtonPanel.class);
    
    
    public MainButtonPanel() {
        //this.setIgnoreRepaint(true);
        setComponentFormatting();
        addButtons();
    }
    
    public MainButtonPanel(int width, int height) {
        this.setSize(new Dimension(width, height));
        setComponentFormatting();
        addButtons();
    }
    
    public MainButtonPanel(Dimension dim) {
        this.setSize(dim);
        setComponentFormatting();
        addButtons();
    }
    
    private void setComponentFormatting() {
        this.setLayout(new FlowLayout());
        this.setVisible(true);
        this.setFocusable(false);
        this.setOpaque(true);
    }
    
    private void addButtons() {
        JButton button1 = formattedButton("area");
        //button1.setMnemonic(KeyEvent.VK_A);
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                mgr.getUiManager().setDialogText(mgr.getUiManager().getDialoguePane().getText() + "\nSwitch to area view");
                if (!button1Clicked) {
                    ((BasicUiManager)mgr.getUiManager()).setAreaViewState();
                    button1Clicked = true;
                }else{
                    ((BasicUiManager)mgr.getUiManager()).setStandardViewState();
                    button1Clicked = false;
                }
                
                
            }
        });
        
        this.add(button1);
        
        JButton button2 = formattedButton("cast");
        button2.setMnemonic(KeyEvent.VK_C);
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                mgr.getUiManager().setDialogText(mgr.getUiManager().getDialoguePane().getText() + "\nCast a Spell");
            }
        });
        
        this.add(button2);
        
        JButton button3 = formattedButton("view");
        button3.setMnemonic(KeyEvent.VK_V);
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                mgr.getUiManager().setDialogText(mgr.getUiManager().getDialoguePane().getText() + "\nView something");
            }
        });
        
        this.add(button3);
        
        JButton button4 = formattedButton("camp");
        button4.setMnemonic(KeyEvent.VK_E);
        button4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                mgr.getUiManager().setDialogText(mgr.getUiManager().getDialoguePane().getText() + "\nMake Camp");
            }
        });
        
        this.add(button4);
        
        JButton button5 = formattedButton("search");
        button5.setMnemonic(KeyEvent.VK_S);
        button5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                mgr.getUiManager().setDialogText(mgr.getUiManager().getDialoguePane().getText() + "\nSearch this area");
            }
        });
        
        this.add(button5);
        
        JButton button6 = formattedButton("look");
        button6.setMnemonic(KeyEvent.VK_L);
        button6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                mgr.getUiManager().setDialogText(mgr.getUiManager().getDialoguePane().getText() + "\nLook at something");
            }
        });
        
        this.add(button6);
        
        JButton button7 = formattedButton("inv");
        button7.setMnemonic(KeyEvent.VK_I);
        button7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                mgr.getUiManager().setDialogText(mgr.getUiManager().getDialoguePane().getText() + "\nView selected inventory");
            }
        });
        
        this.add(button7);
        
    }
    
    //TODO:  Test method, to be removed later
    private JButton formattedButton(String buttonImgName) {
        
        
        String filename = "btn_"+buttonImgName+".png";
        
        JButton newBtn = new JButton(new ImageIcon("resources/images/"+filename));
        //newBtn.setText(labelText);
        //newBtn.setPressedIcon(new ImageIcon("resources/images/button_pressed.png"));
        //newBtn.setRolloverEnabled(true);
        //newBtn.setRolloverIcon(new ImageIcon("resources/images/button_over.png"));
        newBtn.setFocusable(false);
        newBtn.setOpaque(false);
        newBtn.setVisible(true);
        newBtn.setSize(10, 10);
        newBtn.setBorderPainted(false);
        newBtn.setMargin(new Insets(0,0,0,0));
        
        return newBtn;
    }
    
    /**
     * @param gameState
     */
    public void registerGameState(GameStateManager gameState) {
        mgr = gameState;
    }
    
    /**
     * @param gameState
     */
    public void updateGameState(GameStateManager gameState) {
        this.mgr = gameState;
    }
    
}
